<template>
  <div class="xtx-infinite-loading" ref="container">
    <div class="loading" v-if="loading">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <div class="none" v-if="finished">
      <span class="img"></span>
      <span class="text">亲，没有更多了</span>
    </div>
  </div>
</template>

<script>
import { useIntersectionObserver } from '@vueuse/core'
import { ref } from 'vue'
export default {
  name: 'XtxInfiniteLoading',
  props:{
    //判断是否正在加载数据
    loading:{
      type:Boolean,
      default:false
    },
    //判断是否还有数据
    finished:{
      type:Boolean,
      default:false
    }
  },
  setup(props,{ emit }) {
    const container = ref(null)
    useIntersectionObserver(container,([{isIntersecting}])=> {
      //判断是否进入可视区
      if(isIntersecting) {
        //判断触发请求的条件  1.当前没有正再请求 2.还存在更多数据
        if(props.loading == false && props.finished == false) {
          emit('infinite')
        }
      }
    },{
      threshold: 0
    })


    return {
      container
    }
  }
}
</script>

<style scoped lang='less'>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 50px;
      height: 50px;
      background: url(../../assets/image/load.gif) no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 200px;
      height: 134px;
      background: url(../../assets/image/none.png) no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>
